local assets = {
	Asset("ANIM", "anim/musha_piratebag_swap.zip"),
	Asset("ANIM", "anim/musha_armor_shadow.zip")
}

local function Updata(inst, display)
	if inst.components.musha_equipment:IsBroken() then
		inst.components.armor:SetAbsorption(0)
		return
	end

	local armor_value =
		inst.components.musha_equipment.currentlevel < 16 and inst.components.musha_equipment.currentlevel * 2 or
		inst.components.musha_equipment.currentlevel + 15

	inst.components.armor:SetAbsorption(armor_value * 0.01)
	if display then
		inst.components.talker:Say(
			string.format(
				STRINGS.MUSHA_ARMOR_SHADOW .. " (LV%d)\n" .. STRINGS.MUSHA_ARMOR .. " (%d)",
				inst.components.musha_equipment.currentlevel,
				armor_value
			)
		)
	end
end

local function ChangeInsulation(inst)
	if TheWorld.state.issummer then
		inst.components.insulator:SetSummer()
	elseif not TheWorld.state.issummer then
		inst.components.insulator:SetWinter()
	end
end

local function OnBlocked(owner)
	owner.SoundEmitter:PlaySound("dontstarve/wilson/hit_armour")
end

local function OnOpen(inst)
	inst.SoundEmitter:PlaySound("dontstarve/common/gem_shatter")
end

local function OnClose(inst)
	inst.SoundEmitter:PlaySound("dontstarve/common/fireOut")
end

local function OnEquip(inst, owner)
	inst:ListenForEvent("blocked", OnBlocked, owner)

	if inst.musha_spellpower_regen then
		inst.musha_spellpower_regen:Cancel()
	end
	if owner.components.musha_spellpower then
		inst.musha_spellpower_regen =
			inst:DoPeriodicTask(
			0.1,
			function(inst)
				owner.components.musha_spellpower:DoDelta(0.01 + inst.components.musha_equipment.currentlevel * 0.001)
			end
		)
	end

	if inst.components.container ~= nil then
		inst.components.container:Open(owner)
	end

	local switch = {
		["BMM"] = [[owner.AnimState:OverrideSymbol("swap_body_tall", "musha_backpack6", "swap_body_tall")]],
		["BT"] = [[owner.AnimState:OverrideSymbol("swap_body_tall", "musha_backpack4", "swap_body_tall")]],
		["BS"] = [[owner.AnimState:OverrideSymbol("swap_body_tall", "musha_backpack3", "swap_body_tall")]],
		["BM"] = [[owner.AnimState:OverrideSymbol("swap_body_tall", "musha_backpack", "swap_body_tall")]],
		["BL"] = [[owner.AnimState:OverrideSymbol("swap_body_tall", "musha_backpack2", "swap_body_tall")]],
		["WSP"] = [[owner.AnimState:OverrideSymbol("swap_body_tall", "musha_armor_forest_pink", "swap_body_tall")]],
		["WSR"] = [[owner.AnimState:OverrideSymbol("swap_body_tall", "musha_armor_forest_danger", "swap_body_tall")]],
		["WSB"] = [[owner.AnimState:OverrideSymbol("swap_body_tall", "musha_armor_forest_safe", "swap_body_tall")]],
		["WSH"] = [[owner.AnimState:OverrideSymbol("swap_body_tall", "musha_armor_forest", "swap_body_tall")]],
		["WLR"] = [[owner.AnimState:OverrideSymbol("swap_body_tall", "armor_butterfly", "swap_body_tall")]]
	}

	if inst.back_visual ~= "off" then
		loadstring(switch[string.upper(inst.back_visual)])
	end

	local switch = {
		["pirate"] = [[owner.AnimState:OverrideSymbol("swap_body", "musha_armor_shadow", "swap_body")]],
		["green"] = [[owner.AnimState:OverrideSymbol("swap_body", "musha_armor_original", "swap_body")]],
		["pink"] = [[owner.AnimState:OverrideSymbol("swap_body", "musha_armor_princess", "swap_body")]],
		["blue"] = [[owner.AnimState:OverrideSymbol("swap_body", "musha_armor_forest_body", "swap_body")]],
		["chest"] = [[owner.AnimState:OverrideSymbol("swap_body", "musha_piratebag_swap", "backpack")\nowner.AnimState:OverrideSymbol("swap_body", "musha_piratebag_swap", "swap_body")]]
	}

	loadstring(switch[string.lower(inst.armor_visual)])

	inst.defense = function(owner, data)
		if math.random() < 0.5 + 0.01 * inst.components.musha_equipment.currentlevel then
			local tentacle = SpawnPrefab("musha_tentacle_shadow")
			local pos = inst:GetPosition()
			local offset = FindWalkableOffset(pos, math.random(1, 360) * DEGREES, math.random(1, 5), 30, false, false)
			tentacle.Transform:SetPosition(pos.x + offset.x, pos.y, pos.z + offset.z)
			tentacle.components.follower.leader = inst
			tentacle.components.combat:SuggestTarget(data.attacker)
		end
	end

	inst:ListenForEvent("attacked", inst.defense, owner)
end

local function OnUnequip(inst, owner)
	inst:RemoveEventCallback("blocked", OnBlocked, owner)
	owner.AnimState:ClearOverrideSymbol("swap_body")
	owner.AnimState:ClearOverrideSymbol("backpack")
	owner.AnimState:ClearOverrideSymbol("swap_body_tall")

	inst:RemoveEventCallback("attacked", inst.defense, owner)
	if inst.components.container ~= nil then
		inst.components.container:Close(owner)
	end
	if inst.musha_spellpower_regen then
		inst.musha_spellpower_regen:Cancel()
		inst.musha_spellpower_regen = nil
	end
end

local function fn()
	local inst = CreateEntity()

	inst.entity:AddTransform()
	inst.entity:AddAnimState()
	inst.entity:AddSoundEmitter()
	inst.entity:AddNetwork()

	MakeInventoryPhysics(inst)
	MakeInventoryFloatable(inst, nil, nil)

	inst:AddComponent("talker")
	inst.components.talker.fontsize = 30
	inst.components.talker.font = TALKINGFONT
	inst.components.talker.colour = Vector3(0.9, 1, 0.75, 1)
	inst.components.talker.offset = Vector3(0, 150, 0)
	inst.components.talker.symbol = "swap_object"

	inst.AnimState:SetBank("pirate_booty_bag")
	inst.AnimState:SetBuild("musha_piratebag_swap")
	inst.AnimState:PlayAnimation("anim")

	inst:AddTag("backpack")
	inst:AddTag("lowcool")
	inst:AddTag("marble")
	inst:AddTag("aquatic")
	inst:AddTag("musha_items")

	inst.foleysound = "dontstarve/movement/foley/marblearmour"

	inst.entity:SetPristine()

	if TheNet:GetIsClient() then
		inst.OnEntityReplicated = function(inst)
			inst.replica.container:WidgetSetup("krampus_sack")
		end

		return inst
	end

	inst:AddComponent("equippable")
	inst.components.equippable:SetOnEquip(OnEquip)
	inst.components.equippable:SetOnUnequip(OnUnequip)

	inst:AddComponent("musha_equipment")
	inst.components.musha_equipment:SetUpgradable(true)
	inst.components.musha_equipment:InitMushaEquip("armor", 850, 0)
	inst.components.musha_equipment:SetOnUpdataFn(Updata)
	inst.components.musha_equipment:SetOnBrokenFn(
		function()
			if inst.components.armor.absorb_percent ~= 0 then
				inst.components.musha_equipment:StopConsuming()
				inst.components.armor:SetAbsorption(0)
				inst.components.talker:Say(
					STRINGS.MUSHA_ARMOR_BROKEN .. "\n" .. STRINGS.MUSHA_ARMOR .. " (0)\n" .. STRINGS.MUSHA_ITEM_DUR .. " (0)"
				)
			end
			inst.components.musha_equipment:StopConsuming()
		end
	)

	inst.components.inventoryitem.atlasname = "images/inventoryimages/musha_armor_shadow.xml"

	inst.components.equippable.equipslot = EQUIPSLOTS.BACK or EQUIPSLOTS.PACK or EQUIPSLOTS.BODY

	inst:AddComponent("insulator")
	inst.components.insulator:SetInsulation(TUNING.INSULATION_MED)
	ChangeInsulation(inst)

	inst:AddComponent("container")
	inst.components.container:WidgetSetup("krampus_sack")
	inst.components.container.onopenfn = OnOpen
	inst.components.container.onclosefn = OnClose

	inst:AddComponent("leader")

	inst.type = "phoenix"

	return inst
end

return Prefab("musha_armor_shadow", fn, assets)
